package com.yimu.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yimu.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yimu.system.domain.Devices;
import com.yimu.system.mapper.DevicesLogMapper;
import com.yimu.system.domain.DevicesLog;
import com.yimu.system.service.IDevicesLogService;
import com.yimu.common.core.text.Convert;

/**
 * 设备日志Service业务层处理
 * 
 * @author Christine
 * @date 2020-08-26
 */
@Service
public class DevicesLogServiceImpl implements IDevicesLogService 
{
    @Autowired
    private DevicesLogMapper devicesLogMapper;

    /**
     * 查询设备日志
     * 
     * @param Id 设备日志ID
     * @return 设备日志
     */
    @Override
    public DevicesLog selectDevicesLogById(Long Id)
    {
        return devicesLogMapper.selectDevicesLogById(Id);
    }

    /**
     * 查询设备日志列表
     * 
     * @param devicesLog 设备日志
     * @return 设备日志
     */
    @Override
    public List<DevicesLog> selectDevicesLogList(DevicesLog devicesLog)
    {
        return devicesLogMapper.selectDevicesLogList(devicesLog);
    }

    /**
     * 新增设备日志
     * 
     * @param devicesLog 设备日志
     * @return 结果
     */
    @Transactional
    @Override
    public int insertDevicesLog(DevicesLog devicesLog)
    {
        int rows = devicesLogMapper.insertDevicesLog(devicesLog);
        insertDevices(devicesLog);
        return rows;
    }

    /**
     * 修改设备日志
     * 
     * @param devicesLog 设备日志
     * @return 结果
     */
    @Transactional
    @Override
    public int updateDevicesLog(DevicesLog devicesLog)
    {
        devicesLogMapper.deleteDevicesByDeviceNum(devicesLog.getId());
        insertDevices(devicesLog);
        return devicesLogMapper.updateDevicesLog(devicesLog);
    }

    /**
     * 删除设备日志对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDevicesLogByIds(String ids)
    {
        devicesLogMapper.deleteDevicesByDeviceNums(Convert.toStrArray(ids));
        return devicesLogMapper.deleteDevicesLogByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除设备日志信息
     * 
     * @param Id 设备日志ID
     * @return 结果
     */
    @Override
    public int deleteDevicesLogById(Long Id)
    {
        devicesLogMapper.deleteDevicesByDeviceNum(Id);
        return devicesLogMapper.deleteDevicesLogById(Id);
    }

    /**
     * 新增终端列表信息
     * 
     * @param devicesLog 设备日志对象
     */
    public void insertDevices(DevicesLog devicesLog)
    {
        List<Devices> devicesList = devicesLog.getDevicesList();
        Long Id = devicesLog.getId();
        if (StringUtils.isNotNull(devicesList))
        {
            List<Devices> list = new ArrayList<Devices>();
            for (Devices devices : devicesList)
            {
                devices.setId(Id);
                list.add(devices);
            }
            if (list.size() > 0)
            {
                devicesLogMapper.batchDevices(list);
            }
        }
    }
}
